package org.easy.tool.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.easy.tool.common.TimeUtil;
import org.easy.tool.entity.Mark;
import org.easy.tool.entity.Problem;
import org.easy.tool.entity.ProblemMark;
import org.easy.tool.mapper.MarkMapper;
import org.easy.tool.mapper.ProblemMapper;
import org.easy.tool.mapper.ProblemMarkMapper;
import org.easy.tool.service.IMark;
import org.easy.tool.service.IProblem;
import org.easy.tool.service.IProblemMark;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Valley
 * @since 2024-09-04 22:51:14
 */
@Service
public class ProblemMarkService extends ServiceImpl<ProblemMarkMapper, ProblemMark> implements IProblemMark {

    @Autowired
    MarkMapper markMapper;

    @Autowired
    ProblemMapper problemMapper;

    @Override
    public List<Mark> getProblemMarkList(LinkedHashMap data) {
        String problemId = (String) data.get("problemId");
        return getProblemMarkList(problemId);
    }

    @Override
    public List<Mark> getProblemMarkList(String problemId) {
        LambdaQueryWrapper<ProblemMark> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProblemMark::getProblemId, problemId);
        return list(wrapper).stream().map(
                item -> markMapper.selectById(item.getMarkId())
        ).collect(Collectors.toList());
    }

    @Override
    public boolean editProblemMark(LinkedHashMap data) {
        String problemId = (String) data.get("problemId");
        List<String> markList = (List<String>) data.get("markList");
        LambdaQueryWrapper<ProblemMark> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProblemMark::getProblemId, problemId);
        remove(wrapper);
        for (String mark : markList) {
            String addId = markMapper.getMarkByTitle(mark).getId();
            addProblemMark(problemId, addId);
        }
        return true;
    }

    @Override
    public boolean addProblemMark(String problemId, String markId) {
        ProblemMark problemMark = new ProblemMark();
        problemMark.setProblemId(problemId);
        problemMark.setMarkId(markId);
        problemMark.setCreateTime(TimeUtil.now());
        problemMark.setUpdateTime(TimeUtil.now());
        return save(problemMark);
    }

    @Override
    public boolean removeProblemMark(String problemId, String markId) {
        LambdaQueryWrapper<ProblemMark> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProblemMark::getProblemId, problemId);
        wrapper.eq(ProblemMark::getMarkId, markId);
        return remove(wrapper);
    }

    public List<Problem> getProblemList(LinkedHashMap data) {
        Object andList = data.get("andList");
        List<Problem> filterProblem = new ArrayList<>();
        List<String> filterMarkList = (List<String>) andList;
        List<LinkedHashMap> mapList = mergeProblemMark();
        for (LinkedHashMap map : mapList) {
            List<String> problemMarkList = (List<String>) map.get("markList");
            if (problemMarkList.containsAll(filterMarkList)) {
                String problemId = (String) map.get("problemId");
                filterProblem.add(problemMapper.selectById(problemId));
            }
        }
        return filterProblem;
    }

    @Override
    public List<LinkedHashMap> mergeProblemMark() {
        List<Problem> problemList = problemMapper.selectList(new LambdaQueryWrapper<>());
        List<LinkedHashMap> mergeList = new ArrayList<>();
        for (Problem problem : problemList) {
            LinkedHashMap merge = new LinkedHashMap();
            List<Mark> problemMarkList = getProblemMarkList(problem.getId());
            if (problemMarkList.size() > 0) {
                merge.put("problemId", problem.getId());
                List<String> collect = problemMarkList.stream().map(Mark::getTitle).collect(Collectors.toList());
                merge.put("markList", collect);
                mergeList.add(merge);
            }
        }
        return mergeList;
    }


}
